package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLayeredPane;

import java.awt.Color;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.border.TitledBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Input extends JInternalFrame {
	Connection conn;
	Select ss;
	
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JLayeredPane layeredPane;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					Input frame = new Input();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public Input(Connection c) {
		super("입력",true,true,true,true);
		setBounds(100, 100, 373, 233);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLayeredPane_1());
		
		conn=c;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Code");
			lblNewLabel.setBounds(46, 43, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("Ea");
			lblNewLabel_1.setBounds(46, 86, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(115, 37, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(115, 80, 116, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						String c=code.getText();
						int e=Integer.parseInt(ea.getText());
						
						String sql="insert into ptbl(serial, code, ea) values(PTbl_serial.nextval, ?,?)";
						PreparedStatement pstmt=conn.prepareStatement(sql);
						pstmt.setString(1, c);//1번째 물음표 대신에 c값을 넣어라
						pstmt.setInt(2, e);
						
						pstmt.executeUpdate();
						pstmt.close();
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
					
					ss.columnadd();
				}
			});
			btnNewButton.setBounds(118, 123, 97, 23);
		}
		return btnNewButton;
	}
	private JLayeredPane getLayeredPane_1() {
		if (layeredPane == null) {
			layeredPane = new JLayeredPane();
			layeredPane.setBorder(new TitledBorder(null, "\uC785\uB825", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			layeredPane.setBackground(Color.WHITE);
			layeredPane.setBounds(22, 10, 311, 184);
		}
		return layeredPane;
	}
	
	public void setD(Select s){
		this.ss=s;
	}
}
